// pages/navigation/new/query/query.js
const app = getApp()
const select = {
  '类别': ['班级','寝室']
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: [{
      values: select['类别'],
      defaultIndex: 0
    }
    ],
    classmates:[],
    roommates:[],
    select:'班级',
    isConfirm:false
      },
  /**
   * 发起查询
   */
  onConfirm: function(){
    wx.showLoading({
      title: '正在查询',
    })
    if(this.data.select == '寝室'){
      wx.cloud.database().collection('roommate_info')
      .orderBy('bed','asc')
      .get()
      .then(res => {
        this.setData({
          roommates:res.data
        })
        wx.hideLoading()
      })
      .catch(err => {
        console.log('未查找到同寝室成员',err);
      })
    }else{
      wx.cloud.database().collection('classmate_info')
      .orderBy('sno','asc')
      .get()
      .then(res => {
        console.log('查找到同班同学',res.data);
        this.setData({
          classmates:res.data
        })
        wx.hideLoading();
      })
      .catch(err => {
        console.log('未查找到同班同学',err);
      })
    }
    this.setData({
      isConfirm:true
    })
  },
  onChange: function(e){
    this.setData({
      select:e.detail.value,
      isConfirm:false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
})